package com.sky.controller.user;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Constants;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "地址簿相关接口")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;


    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    @ApiOperation("新增地址")
    public Result addAddressBook(@RequestBody AddressBook addressBook) {
        log.info("新增地址：{}", addressBook);
        addressBookService.addAddressBook(addressBook);
        return Result.success();
    }

    /**
     * 查询该用户的所有地址
     * @return
     */
    @GetMapping("list")
    @ApiOperation("查询该用户的所有地址")
    public Result<List<AddressBook>> list(){
        log.info("查询该用户的所有地址");
        AddressBook addressBook = AddressBook.builder()
                .userId(BaseContext.getCurrentId()).build();
        List<AddressBook> lists = addressBookService.list(addressBook);
        return Result.success(lists);
    }


    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("default")
    @ApiOperation("设置默认地址")
    public Result setDefault(@RequestBody AddressBook addressBook){
        log.info("设置默认地址:{}",addressBook);
        addressBookService.setDefault(addressBook);
        return Result.success();
    }


    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> getDefault(){
        log.info("查询默认地址");
        AddressBook addressBook = AddressBook.builder()
                .userId(BaseContext.getCurrentId())
                .isDefault(1).build();
        List<AddressBook> list = addressBookService.list(addressBook);
        if (list != null && list.size() > 0){
            addressBook = list.get(0);
        }else {
            Result.error(MessageConstant.ADDRESS_NOT_FOUND);
        }
        return Result.success(addressBook);
    }


    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> getAddressBooKById(@PathVariable("id") Long id){
        log.info("根据id查询地址:{}", id);
        AddressBook addressBook = addressBookService.getAddressBooKById(id);
        return Result.success(addressBook);
    }


    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation("修改地址")
    public Result update(@RequestBody AddressBook addressBook){
        log.info("修改地址:{}", addressBook);
        addressBookService.update(addressBook);
        return Result.success();
    }


    @DeleteMapping
    @ApiOperation("删除地址")
    public Result delete(Long id){
        log.info("删除地址:{}", id);
        addressBookService.deleteById(id);
        return Result.success();
    }

}
